<?php
  namespace app\admin\controller;
  class Brand extends \think\Controller{
  	public $mec;
  	function __construct(){
  		parent::__construct();
        $this->mec=new \app\admin\controller\Memcache();
  	}
  	function brand_mem(){
	        $list=\think\Db::name('brand')->select();
	        $list_=[];
	        foreach($list as $v){
	        	 $list_[$v['id']]=$v;
	        }
            $this->mec->mem->set('brand',$list_);
}
 function files(){
		   if(!$_FILES['file']['error']){
             $fg=$_FILES['file']['name'];
             $filename="upload/".$fg;
          if(move_uploaded_file($_FILES['file']['tmp_name'],$filename)){
            echo json_encode(['status'=>'ok','file_url'=>$fg]);
          }else{
          	 echo json_encode(['status'=>'fail']);
          }
       }
	 }

  	  function index(){
  	  	  if(!$brand=$this->mec->mem->get('brand')){
  	  	  	  $this->brand_mem();
  	  	  }
  	  	  $this->assign('brand',$this->mec->mem->get('brand'));
  	  	  return $this->fetch('index/brand_data');
  	  }

  	  function brand_add(){
  	  	 if(input('get.id')){
  	  	 	$res=\think\Db::name('brand')->find(input('get.id'));
  	  	 	$this->assign('row',$res);
  	  	 }
  	  	 return $this->fetch('index/brand_add');
  	  }

  	  function brand_save(){
  	  	 $res=\think\Db::name('brand')->insert(input('post.'));
         if($res){
         	 $this->brand_mem();
         	 echo JSON_encode(['status'=>'ok']);
         }else{
         	 echo JSON_encode(['status'=>'fail']);
         }
  	  }

  	  function brand_edit(){
  	  	 $res=\think\Db::name('brand')->where('id',input('get.id'))->update(input('post.'));
         if($res){
         	 $this->brand_mem();
         	 echo JSON_encode(['status'=>'ok']);
         }else{
         	 echo JSON_encode(['status'=>'fail']);
         }
  	  }

  	  function brand_delt(){
  	  	 $res=\think\Db::name('brand')->delete(input('post.id'));
         if($res){
         	 $this->brand_mem();
         	 echo JSON_encode(['status'=>'ok']);
         }else{
         	 echo JSON_encode(['status'=>'fail']);
         }
  	  }
  }
?>